<template>
  <div class="article-container">
    <el-card class="box-card"
             v-loading="loading">
      <div slot="header"
           class="head-box">
        <span class="title">文章类别管理</span>
        <el-button type="primary"
                   class="btn"
                   @click="openAdd">添加分类</el-button>
      </div>
      <div class="text-box">
        <el-table :data="list"
                  border
                  style="width: 100%"
                  row-key="id">
          <el-table-column type="index"
                           label="序号"
                           width="150">
          </el-table-column>
          <el-table-column label="项目名称"
                           min-width="40%"
                           prop="cate_name">
          </el-table-column>
          <el-table-column label="分类别名"
                           min-width="40%"
                           prop="cate_alias">
          </el-table-column>
          <el-table-column label="操作"
                           min-width="20%">
            <template v-slot="{ row }">
              <el-button size="mini"
                         class="edit-btn"
                         @click="handleEdit(row)">修改</el-button>
              <el-button size="mini"
                         type="danger"
                         class="del-btn"
                         @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <add-cate ref="AddCate"
                :showdialog.sync="showadddialog"
                @getArtCate="getArtCate"></add-cate>
      <edit-cate ref="EditCate"
                 :showdialog.sync="showeditdialog"
                 :useid="useid"></edit-cate>
    </el-card>
  </div>
</template>

<script>
import { getArtCate, delArtCate } from '@/api/article'
import AddCate from './components/add-cate'
import EditCate from './components/edit-cate'
export default {
  name: 'ArtCatePage',
  data () {
    return {
      list: [], // 接收分类列表数据
      fromData: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' },
          { min: 1, max: 10, message: '名称长度为1-10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名不能为空', trigger: 'blur' },
          { min: 1, max: 15, message: '名称长度为1-15位', trigger: 'blur' }
        ]
      },
      loading: false,
      showadddialog: false,
      showeditdialog: false,
      useid: ''
    }
  },
  components: {
    AddCate, EditCate
  },
  created () {
    this.getArtCate()
  },
  methods: {
    openAdd () {
      this.showadddialog = true
    },
    async handleEdit (id) {
      // 我应该怎么给组件传递ID值
      // this.editinfo = await getCate(id)
      // console.log(typeof this.editinfo.data.data)
      // this.useid = id
      // console.log(this.useid)
      // this.showeditdialog = true
    },
    async handleDelete (id) {
      try {
        await this.$confirm('确认删除吗？')
        await delArtCate(id)
        this.getArtCate()
        this.$message.success('删除成功')
        this.getArtCate()
      } catch (err) {
        console.log(err)
      }
    },
    async getArtCate () {
      this.loading = true
      const res = await getArtCate()
      // console.log(res.data.data)
      // this.list.cate_name = res.data.data.cate_name
      // this.list.cate_alias = res.data.data.cate_alias
      this.list = res.data.data
      // console.log(this.list)
      this.loading = false
    }
  }

}
</script>

<style lang="less" scoped>
.article-container {
  .box-card {
    width: 100%;

    .el-card__header {
      height: 48px;

      .title {
        font-size: 18px;
        line-height: 48px;
        font-weight: 600;
      }

      .btn {
        float: right;
        margin-top: 5px;
        font-size: 14px;
        background-color: #4f81ff;
      }
    }

    .text-box {
      padding: 10px;
      .el-table {
        font-size: 14px;
        // color: #e7e7e7;
      }
      .edit-btn {
        color: #fff;
        font-size: 14px;
        background-color: #4f81ff;
      }
      .del-btn {
        color: #fff;
        font-size: 14px;
      }
    }
  }
}
</style>
